<?php
class Register extends CI_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('crm_model');
		$this->load->helper(array('form', 'url'));
		$this->load->library('email');
	}
	
	public function index($page = 'register')
	{
	
		if ( ! file_exists('application/views/crm/'.$page.'.php'))
		{
			// 页面不存在
			show_404();
		}
		$this->load->view('crm/register');
	}
	
	public function registe()
	{
		$user_name = $this->input->post('username');
		$password = $this->input->post('password');
		$email = $this->input->post('email');
		$status = '';
		$activationKey = trim(com_create_guid(),'{}');
		$user_info = array(
					  'email' => $email,
					  'user_name' => $user_name,
					  'password' => $password,
					  'status' => $status,
					  'activationkey' => $activationKey
					);
		$this->crm_model->register_user($user_info);
		$this->email->from('545616089@qq.com','企搜');
		$this->email->to($email);
		$this->email->subject('注册验证');
		$message = "<html><body>";
		$message .= '<a href="'.base_url().'register/verify/'.$activationKey.'">点击我</a>'; 
		$message .= '如果不能正常打开，请复制下面的地址到浏览器：<p style="color:#00f">'.base_url().'register/verify/'.$activationKey;
		$message .= "</body></html>";
		$this->email->message($message);
		$this->email->send();
		
		$data['message'] = '';
		$this->load->view('crm/regsuccess', $data);
	}
	
	public function verify(){
		$activationKey = $this->uri->segment(3);
		$result = $this->crm_model->get_all_user();
		foreach ($result as $row)
		{
			if ($activationKey == $row['activationkey'])
			{
				$data['message'] =  "恭喜你!" . $row["user_name"] . "已经激活，现在可以返回登录了";
				$this->crm_model->update_user($row['id']);
				$this->load->view('crm/ok', $data);
			}
		}
	}
}